package com.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.usercarshow.MainActivity;
import com.usercarshow.R;

public class ExhibitHot extends Activity {
	// "主页" 按钮
	private Button home;
	private GridView car_nav;
	private ListView mListView;
	static ExhibitHot exhibitHot;

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExhibitHot.exhibitHot = this;
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
		setContentView(R.layout.exhibit_hot);
		loadAllComponent();
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return true;
	}

	/** 加载组件 */
	private void loadAllComponent() {
		home = (Button) findViewById(R.id.home);
		// home.setOnTouchListener(onTouchListener);
		car_nav = (GridView) findViewById(R.id.car_nav);
		car_nav.setAdapter(new ImageAdapter(this));
		car_nav.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			}
		});

		mListView = (ListView) findViewById(R.id.listView);
		mListView.setAdapter(new SimpleAdapter(this, getData(),
				R.layout.list_view_image, new String[] { "img" },
				new int[] { R.id.image }));
		mListView.setOnItemClickListener(itemClickListener);
		home.setOnClickListener(clickListener);

	}

	private OnTouchListener onTouchListener = new OnTouchListener() {
		@Override
		public boolean onTouch(View view, MotionEvent e) {
			if (e.getAction() == MotionEvent.ACTION_DOWN) {
			} else if (e.getAction() == MotionEvent.ACTION_UP) {
				if (view.getId() == R.id.home) { // 返回主页
					home.setAnimation(AnimationUtils.loadAnimation(
							ExhibitHot.this, R.anim.button_anim));
					Intent intent = new Intent(ExhibitHot.this,
							MainActivity.class);
					ExhibitHot.this.startActivity(intent);
					ExhibitHot.this.overridePendingTransition(
							android.R.anim.fade_in, android.R.anim.fade_out);
				}
			}
			return true;
		}
	};

	private View.OnClickListener clickListener = new View.OnClickListener() {
		@Override
		public void onClick(View view) {
			Log.i("ff","ff");
			if (view.getId() == R.id.home) { // 返回主页
				home.setAnimation(AnimationUtils.loadAnimation(ExhibitHot.this,
						R.anim.button_anim));
				Intent intent = new Intent(ExhibitHot.this, MainActivity.class);
				ExhibitHot.this.startActivity(intent);
				ExhibitHot.this.overridePendingTransition(
						android.R.anim.fade_in, android.R.anim.fade_out);
			}
		}
	};

	/** 单击事件监听器 */
	private OnItemClickListener itemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> list, View view, int position,
				long id) {
			Intent intent = new Intent(MainActivity.main,
					ExhibitDetailPanel.class);
			MainActivity.main.startActivity(intent);
			MainActivity.main.overridePendingTransition(android.R.anim.fade_in,
					android.R.anim.fade_out);
		}

	};

	private List<Map<String, Object>> getData() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", R.drawable.car_show1);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.car_show2);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.car_show3);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.car_show4);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.car_show5);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("img", R.drawable.car_show6);
		list.add(map);

		return list;
	}

	class ImageAdapter extends BaseAdapter {
		private Context mContext;
		// 定义热门分类图标
		private Integer[] mImageIds = { R.drawable.hort_sort1,
				R.drawable.hort_sort2, R.drawable.hort_sort3,
				R.drawable.hort_sort4, R.drawable.hort_sort5, };

		public ImageAdapter(Context context) {
			this.mContext = context;
		}

		@Override
		public int getCount() {
			return mImageIds.length;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			ImageView imageView;
			if (view == null) {
				imageView = new ImageView(mContext);
			} else {
				imageView = (ImageView) view;
			}
			imageView.setImageResource(mImageIds[position]);
			return imageView;
		}

	}

}
